import java.util.ArrayList;
import java.util.List;

/**
 * 题目链接: https://leetcode.cn/problems/permutations/description/
 */
class Solution1 {
    List<List<Integer>> RET;
    List<Integer> PATH;
    boolean[] check;//实现剪枝

    public List<List<Integer>> permute(int[] nums) {
        RET = new ArrayList<>();
        PATH = new ArrayList<>();
        check = new boolean[nums.length];
        dfs(nums);
        return RET;
    }

    public void dfs(int[] nums) {
        if (PATH.size() == nums.length) {
            RET.add(new ArrayList<>(PATH));
            return;
        }
        // 枚举数组所有的元素
        for (int i = 0; i < nums.length; i++) {
            // 如果i位置没有用过
            if (check[i] == false) {
                PATH.add(nums[i]);
                check[i] = true;
                dfs(nums);
                // 恢复现场
                check[i] = false;
                PATH.remove(PATH.size() - 1);
            }
        }
    }
}